package com.edee.bigday.seckill.controller;

import com.edee.bigday.seckill.service.SecKillService;
import com.edee.bigday.seckill.to.SeckillSkuRedisTo;
import com.edee.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController
@Controller
public class SeckillController {

    @Autowired
    SecKillService secKillService;

    /**
     * 获取当前时间可以参与秒杀的商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/get_current_seckill_skus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> seckillSkuRedisTos = secKillService.getCurrentSeckillSkus();
        return R.ok().setData(seckillSkuRedisTos);
    }

    /**
     * 根据商品ID查询秒杀信息
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/info/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo seckillSkuRedisTo = secKillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(seckillSkuRedisTo);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = secKillService.kill(killId, key, num);
        model.addAttribute("orderSn",orderSn);
        //1、判断是否登录
        return "success";
    }
}
